# -*- coding: utf-8 -*-
"""
现代化主题配置文件 (重构版)
"""


class ThemeManager:
    """
    主题管理器 (重构版)
    将基础样式与颜色配置分离，实现真正的样式统一
    """

    def __init__(self, main_window):
        self.main_window = main_window
        self.current_theme = "ocean"

    @staticmethod
    def get_theme_colors(theme_name):
        """
        [核心修改] 定义每个主题的颜色配置
        """
        themes = {
            "ocean": {
                # 背景渐变
                "bg_start": "#001122", "bg_mid1": "#003366", "bg_mid2": "#004080", "bg_end": "#0066cc",
                # 主色调
                "primary": "#00ccff", "primary_light": "#00ffff", "primary_dark": "#0080ff",
                # 组件背景
                "component_bg": "rgba(0, 51, 102, 0.7)", "component_bg_dark": "rgba(0, 17, 34, 0.9)",
                # 文本颜色
                "text_light": "#ffffff", "text_dark": "#000000",
                # 边框颜色
                "border_color": "rgba(0, 204, 255, 0.5)", "border_color_strong": "rgba(0, 204, 255, 0.7)",
                # 特殊按钮颜色
                "save_btn_start": "#ffd700", "save_btn_end": "#ff8c00",
                "exit_btn_start": "#ff6b6b", "exit_btn_end": "#ee5a52",
            },
            "cyber": {
                "bg_start": "#0a0a0a", "bg_mid1": "#1a1a2e", "bg_mid2": "#16213e", "bg_end": "#0f3460",
                "primary": "#00ffff", "primary_light": "#ffffff", "primary_dark": "#00ffff",
                "component_bg": "rgba(26, 26, 46, 0.8)", "component_bg_dark": "rgba(10, 10, 10, 0.9)",
                "text_light": "#00ffff", "text_dark": "#00ffff",
                "border_color": "rgba(0, 255, 255, 0.5)", "border_color_strong": "rgba(0, 255, 255, 0.7)",
                "save_btn_start": "#ffff00", "save_btn_end": "#ffff00",
                "exit_btn_start": "#ff0080", "exit_btn_end": "#ff0080",
            },
            "eco": {
                "bg_start": "#0a2a0a", "bg_mid1": "#1a4a1a", "bg_mid2": "#2a6a2a", "bg_end": "#3a8a3a",
                "primary": "#32cd32", "primary_light": "#7fff00", "primary_dark": "#228b22",
                "component_bg": "rgba(42, 106, 42, 0.7)", "component_bg_dark": "rgba(10, 42, 10, 0.9)",
                "text_light": "#ffffff", "text_dark": "#ffffff",
                "border_color": "rgba(50, 205, 50, 0.5)", "border_color_strong": "rgba(50, 205, 50, 0.7)",
                "save_btn_start": "#ffd700", "save_btn_end": "#ff8c00",
                "exit_btn_start": "#ff6b6b", "exit_btn_end": "#ee5a52",
            }
        }
        return themes.get(theme_name, themes["ocean"])

    @staticmethod
    def get_base_style():
        """
        [核心修改] 定义统一的基础样式模板，不包含颜色和字体大小
        使用占位符 {color_name} 来等待填充
        """
        return """
            QWidget#centralwidget {{
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                    stop:0 {bg_start}, stop:0.3 {bg_mid1}, 
                    stop:0.7 {bg_mid2}, stop:1 {bg_end});
                color: {text_light};
            }}
            QWidget#header_widget {{
                background: {component_bg};
                border-radius: 12px;
            }}
            QGroupBox {{
                background: {component_bg};
                border: 2px solid {border_color};
                border-radius: 15px;
                color: {primary};
                margin-top: 15px;
                padding-top: 10px;
            }}
            QGroupBox::title {{
                subcontrol-origin: margin;
                left: 15px;
                padding: 5px 15px;
                color: {primary};
                background: {component_bg_dark};
                border: 1px solid {border_color_strong};
                border-radius: 8px;
            }}
            QPushButton {{
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 {primary_dark}, stop:1 {primary});
                border: none;
                border-radius: 12px;
                color: {text_light};
                padding: 12px 20px;
                min-height: 40px;
            }}
            QPushButton:hover {{
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 {primary}, stop:1 {primary_light});
            }}
            #SaveBtn {{
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 {save_btn_start}, stop:1 {save_btn_end});
                color: #000000;
            }}
            #ExitBtn {{
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 {exit_btn_start}, stop:1 {exit_btn_end});
            }}
            QTableWidget {{
                background-color: {component_bg};
                border: 1px solid {border_color};
                border-radius: 10px;
                color: {text_light};
                selection-background-color: {primary};
            }}
            QHeaderView::section {{
                background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 {primary}, stop:1 {primary_dark});
                color: {text_light};
                padding: 12px;
                border: none;
            }}
            QDoubleSpinBox, QComboBox, QLineEdit {{
                background-color: {component_bg};
                border: 2px solid {border_color};
                border-radius: 8px;
                padding: 8px 12px;
                color: {text_light};
            }}
            QDoubleSpinBox:focus, QComboBox:focus, QLineEdit:focus {{
                border: 2px solid {primary};
            }}
            QCheckBox {{ color: {text_light}; }}
            QLabel {{ color: {text_light}; }}
            #label_3 {{ color: {primary}; }}
            #label_show {{
                background-color: {component_bg};
                border: 2px dashed {border_color};
                border-radius: 12px;
                color: {primary};
            }}
        """

    def apply_theme(self, theme_name):
        """应用主题"""
        colors = self.get_theme_colors(theme_name)
        base_style = self.get_base_style()

        # 将颜色配置填充到基础样式模板中
        final_stylesheet = base_style.format(**colors)

        self.main_window.setStyleSheet(final_stylesheet)
        self.current_theme = theme_name
        print(f"Applied theme: {theme_name}")

    def get_current_theme(self):
        return self.current_theme

    def get_available_themes(self):
        return ["ocean", "cyber", "eco"]
